package com.mnemonic;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import com.mnemonic.domain.Game;
import com.mnemonic.domain.Level;
import com.mnemonic.domain.Player;

/**
 * 启动类
 * 
 * @author lifeng
 * @date 2021/05/15
 */
public class MainPage {

    /**
     * 主窗体
     */
    private JFrame root;

    /**
     * 难度的 list
     */
    private JList<Level> levelList;
    private DefaultListModel<Level> levelModel;

    /**
     * 玩家的 list
     */
    private JList<Player> playerList;
    private DefaultListModel<Player> playerModel;

    /**
     * 存档的 list
     */
    private JList<Game> gameList;
    private DefaultListModel<Game> gameModel;

    public MainPage() {
        root = new JFrame();
        root.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        double h = 500;
        double w = h * 1.66;

        // 居中显示
        root.setBounds(0, 0, (int)w, (int)h);
        root.setLocationRelativeTo(null);

        // 设置显示边框和title栏
        root.setUndecorated(false);

        // 设置不能调整大小
        root.setResizable(false);

        // 这段设置图标
        root.setIconImage(Images.logo);

        // 这段设置布局
        root.getContentPane().setLayout(new BorderLayout(0, 0));

        // 设置标题
        root.setTitle("扫雷");

        // ########## 中间部分 ###########
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(null);

        // 选择难度
        JLabel label1 = new JLabel("选择难度（选择一个难度）：");
        label1.setBounds(10, 10, 500, 20);
        mainPanel.add(label1);

        // 难度列表
        levelList = new JList<>();
        levelModel = new DefaultListModel<>();
        levelList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        levelList.setSelectedIndex(0);
        levelList.setModel(levelModel);
        for (Level level : Levels.getLevels()) {
            this.levelModel.addElement(level);
        }
        JScrollPane ps = new JScrollPane(levelList);
        ps.setBounds(10, 40, 500, 80);
        mainPanel.add(ps);

        // 选择玩家
        JLabel label2 = new JLabel("选择玩家（按住 ctrl + 左键 ，至少选择两个玩家）：");
        label2.setBounds(10, 130, 500, 20);
        mainPanel.add(label2);

        // 玩家列表
        playerList = new JList<>();
        playerModel = new DefaultListModel<>();
        playerList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        playerList.setSelectedIndex(0);
        playerList.setModel(playerModel);
        for (Player player : Players.getPlayers()) {
            this.playerModel.addElement(player);
        }
        ps = new JScrollPane(playerList);
        ps.setBounds(10, 160, 500, 180);
        mainPanel.add(ps);

        // 选择存档
        JLabel label3 = new JLabel("选择一个存档继续游戏：（点击刷新存档加载新的存档）：");
        label3.setBounds(530, 10, 280, 20);
        mainPanel.add(label3);

        // 存档列表
        gameList = new JList<>();
        gameModel = new DefaultListModel<>();
        gameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        gameList.setSelectedIndex(0);
        gameList.setModel(gameModel);
        ps = new JScrollPane(gameList);
        ps.setBounds(530, 40, 280, 300);
        mainPanel.add(ps);

        // 开始
        JButton play = new JButton("开始游戏");
        play.addActionListener((e) -> {
            this.doPlay();
        });
        play.setBounds(10, 350, 100, 40);
        mainPanel.add(play);

        // 自定义难度
        JButton config = new JButton("自定义难度");
        config.addActionListener((e) -> {
            this.doConfig();
        });
        config.setBounds(120, 350, 100, 40);
        mainPanel.add(config);

        // 加载存档
        JButton load = new JButton("加载存档");
        load.setBounds(230, 350, 100, 40);
        load.addActionListener((e) -> {
            this.doLoad();
        });
        mainPanel.add(load);

        // 刷新存档
        JButton refresh = new JButton("刷新存档");
        refresh.setBounds(340, 350, 100, 40);
        refresh.addActionListener((e) -> {
            this.doRefresh();
        });
        mainPanel.add(refresh);

        root.add(mainPanel, BorderLayout.CENTER);
        // ############################

        // 关闭
        root.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int result = Dialogs.confirm("提醒", "确认关闭？");
                if (result == 0) {
                    MainPage.this.destory();
                }
            }
        });

        // 显示
        root.setVisible(true);
        root.setExtendedState(JFrame.NORMAL);
    }

    /**
     * 点击： 开始游戏
     */
    private void doPlay() {

        // 选择的难度
        int index = this.levelList.getSelectedIndex();
        if (index == -1) {
            Dialogs.error("错误提醒", "请选择难度!");
            return;
        }
        Level level = Levels.getLevels().get(index);

        // 选择的玩家
        int[] indexs = this.playerList.getSelectedIndices();
        if (indexs == null || indexs.length < 2) {
            Dialogs.error("错误提醒", "至少需要选择两名玩家！");
            return;
        }
        List<Player> players = new ArrayList<>();
        for (int i : indexs) {
            players.add(Players.getPlayers().get(i));
        }

        // 开始游戏
        this.startGame(Game.newGame(level, players));
    }

    /**
     * 开始游戏
     * 
     * @param game
     */
    private void startGame(Game game) {
        new GamePage(root, game).setVisible(true);
    }

    /**
     * 点击： 自定义难度
     */
    private void doConfig() {

    }

    /**
     * 点击： 加载存档
     */
    private void doLoad() {

    }

    /**
     * 点击： 刷新存档
     */
    private void doRefresh() {

    }

    /**
     * 实际的关闭
     */
    private void destory() {
        root.dispose();
    }

    /**
     * 启动的main 方法
     * 
     * @param args
     */
    public static void main(String[] args) {
        new MainPage();
    }
}
